﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Linq.Expressions;

namespace SimpleData
{
	public class DataGenEntityBuilder<TEntity>
	{
		public DataGenEntityBuilder( DGen dgenBuilder, TEntity entity )
			: this( dgenBuilder, null, entity )
		{ }

		public DataGenEntityBuilder( DGen dgenBuilder, ObjectContext context, TEntity entity )
		{
			this.Entity = entity;
			this.DGen = dgenBuilder;

			if ( context == null )
				this.Context = dgenBuilder.NewRepository();
			else
				this.Context = context;
		}

		public TEntity Entity
		{
			get;
			private set;
		}

		public ObjectContext Context
		{
			get;
			private set;
		}

		public DGen DGen
		{
			get;
			private set;
		}


		public DataGenEntityBuilder<TEntity> SetPrivateProperty<TValue>( Expression<Func<TEntity, TValue>> set, TValue value )
		{
			throw new NotImplementedException();
		}

		public TEntity Save()
		{
			try
			{
				this.Context.SaveChanges();
				return this.Entity;
			}
			finally
			{
				this.Context.Dispose();
			}
		}
	}

}
